package controller

import (
	"hft/Database/admin"
	"hft/Http/Models/request"
	services "hft/Http/Services"
	utils "hft/Http/Utils"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

type LouPanName struct{}

func (*LouPanName) Create(c *gin.Context) {

	var data *admin.LouPanName

	c.ShouldBindJSON(&data)

	// 构建所属区域

	err := services.LouPanNameServices.Create(data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*LouPanName) Edit(c *gin.Context) {

	var data *admin.LouPanName

	c.ShouldBindJSON(&data)

	err := services.LouPanNameServices.Edit(data)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*LouPanName) Delete(c *gin.Context) {

	var ids *request.IDS

	c.ShouldBindJSON(&ids)

	err := services.LouPanNameServices.Delete(ids.IDS)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.Ok(c)

}

func (*LouPanName) List(c *gin.Context) {

	var paginate request.LouPanNamePage

	c.ShouldBind(&paginate)

	list, err := services.LouPanNameServices.List(&paginate)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*LouPanName) ListByAccount(c *gin.Context) {

	var paginate request.LouNameAccountPage

	c.ShouldBind(&paginate)

	claims, err := tools.GetClaims(c)

	if err != nil {
		utils.FailWithMessage("授权失败", c)
		return
	}

	list, err := services.LouPanNameServices.GetLouNameByAccount(&paginate, claims.BaseClaims)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

var LouPanNameController = new(LouPanName)
